package backTracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

//46. 全排列
public class Num46_permute {
    List<List<Integer>>res = new ArrayList<>();
    LinkedList<Integer>path = new LinkedList<>();
    boolean []used;
    public List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0)return res;
        used = new boolean[nums.length];
        backTracking(nums);
        return res;
    }

    private void backTracking(int[] nums) {
        if (path.size() == nums.length){
            res.add(new LinkedList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (used[i]){
                continue;
            }else {
                used[i] = true;
                path.add(nums[i]);
                backTracking(nums);
                path.removeLast();
                used[i] = false;
            }
        }

    }

}
